module.exports = class extends baseController {
    constructor() {
        super();
        this.multiparty = require("multiparty");
        this.router.all('/config', this.getConfig.bind(this));
        this.router.post('/image', this.uploadImage.bind(this));
        this.router.post('/file', this.uploadFile.bind(this));
        this.router.post('/bigFile', this.uploadBigFile.bind(this));
        return this.router;
    }

    static init() {
        return new this();
    }


    getConfig(req, res) {
        let config = require('../upload-handler/config');
        res.json(config);
    }

    async uploadImage(req, res) {
        let contentType = req.query.contentType || '';
        if (contentType == 'base64') {
            await require('../upload-handler/upload-scrawl-handler')(req, res).handler();
        } else {
            await require('../upload-handler/upload-image-handler')(req, res).handler();
        }
    }

    async uploadFile(req, res) {
        await require('../upload-handler/upload-file-handler')(req, res).handler();
    }

    async uploadBigFile(req, res) {
        let form = new this.multiparty.Form({
            autoFields: true,
            autoFiles: false,
        });

        form.on('part', (part) => {
            form.on('aborted', () => {
                //意外退出或者暂停都会保存数据 
                console.log('aborted');

            });
        });
    }

}